package com.google.denta.utils;

/**
 * @author Andrei_Leonau
 */
public class Pages {
    /** Pages lines count. */
    private Long lines;

    /** Valid keys. */
    private String[] args;

    /**
     * Constructor.
     * @param lines - pages lines count
     */
    public Pages(Long lines) {
        this.lines = lines;
    }

    /**
     * Constructor.
     * @param lines - pages lines count
     * @param args - valid keys
     */
    public Pages(Long lines, String... args) {
        this.lines = lines;
        this.args = args;
    }

    /**
     * @param items Long
     * @return Long
     */
    public final Long count(Long items) {
        if (items == null) {
            items = 0L;
        }
        return (items + lines - 1L) / lines;
    }

    /**
     * @param page Long
     * @return Long
     */
    public final Long offset(Long page) {
        if (page == null) {
            page = 1L;
        }
        return (page - 1L) * lines;
    }

    /**
     * @param key String
     * @return String
     */
    public final String valid(String key) {
        if (args != null) {
            for (String arg : args) {
                if (arg.equals(key)) {
                    return key;
                }
            }
        }
        return null;
    }

    /**
     * @return Long page count lines
     */
    public final Long lines() {
        return lines;
    }
}
